package com.pmpd.activity.controller.api;

import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import com.pmpd.activity.controller.api.param.ActivityProgressParam;
import com.pmpd.activity.controller.api.param.ActivityWinningParam;
import com.pmpd.activity.po.ActivityWinning;
import com.pmpd.activity.service.api.ApiActivityWinningService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author liuying
 * @Description 活动获奖人信息控制层
 * @Date 15:08 2018/6/7
 */
@Api(value = "App/活动中奖API", description = "App/活动中奖API")
@RestController
@RequestMapping(value = "/api/pmpd-activity-server/v1/activity/activityWinning")
public class ApiActivityWinningController {

    @Resource
    private ApiActivityWinningService apiActivityWinningService;

    @ResponseBody
    @ApiOperation(value = "添加奖品发放信息", notes = "1. 如果返回data为null，则为用户添加领奖信息;\n" +
            "\n" +
            "2. 如果返回data为userId，则为用户已添加过领奖信息")
    @RequestMapping(value = "/add/addWinnerMassage", method = RequestMethod.POST)
    public BaseVo addWinnerMassage(@RequestBody ActivityWinningParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isEmpty(param.getActivityId()) || Long.parseLong(param.getActivityId()) == 0
                || StringUtils.isEmpty(param.getUserId()) || Long.parseLong(param.getUserId()) == 0
                || StringUtils.isEmpty(param.getUserName()) || StringUtils.isEmpty(param.getPhone())
                || StringUtils.isEmpty(param.getAddr())) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }

        int awardCount = apiActivityWinningService.isAward(Long.parseLong(param.getActivityId()), Long.parseLong(param.getUserId()));

        if (awardCount < 1){
            //没有领奖信息则表明该用户领奖
            ActivityWinning winning = apiActivityWinningService.addWinnerMassage(param);
            if (winning == null){
                baseVo.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
                baseVo.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
                return baseVo;
            }
            baseVo.setData(null);
        } else {
            baseVo.setData(param.getUserId());
        }

        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "判断是否已领奖", notes = "1. 参数中type不填;")
    @RequestMapping(value = "/select/isAward", method = RequestMethod.POST)
    public BaseVo isAward(@RequestBody ActivityProgressParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isEmpty(param.getActivityId()) || Long.parseLong(param.getActivityId()) == 0
                || StringUtils.isEmpty(param.getUserId()) || Long.parseLong(param.getUserId()) == 0) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }

        int awardCount = apiActivityWinningService.isAward(Long.parseLong(param.getActivityId()), Long.parseLong(param.getUserId()));

        if (awardCount < 1){
            baseVo.setData(false);
        } else {
            baseVo.setData(true);
        }
        return baseVo;
    }

}
